package com.irain.pp.tool.mybatis.spring;

import com.irain.pp.tool.api.transaction.TransactionCenter;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.DefaultTransactionStatus;

import javax.sql.DataSource;


/**
 * @author iRain
 * @date 2017/12/6 19:41
 */
public class PpDataSourceTransactionManager extends DataSourceTransactionManager {

    public PpDataSourceTransactionManager() {
    }

    public PpDataSourceTransactionManager(DataSource dataSource) {
        super(dataSource);
    }

    @Override
    protected void doBegin(Object transaction, TransactionDefinition definition) {
        TransactionCenter.doAllBeginBefore();
        super.doBegin(transaction, definition);
        TransactionCenter.doAllBeginAfter();
    }

    @Override
    protected void doCommit(DefaultTransactionStatus status) {
        TransactionCenter.doAllCommitBefore();
        super.doCommit(status);
        TransactionCenter.doAllCommitAfter();
    }

    @Override
    protected void doRollback(DefaultTransactionStatus status) {
        TransactionCenter.doAllRollBackBefore();
        super.doRollback(status);
        TransactionCenter.doAllRollBackAfter();
    }
}
